package com.touringapp.android.activity;

import java.util.ArrayList;
import java.util.Iterator;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.touringapp.android.R;
import com.touringapp.android.overlays.RouteOverLay;

public class MapsActivity extends MapActivity {
	public static String MAP = "com.touringapp.android.maps";
	MapView mapView;
	MapController mc;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map);
		mapView = (MapView) findViewById(R.id.mapView);

		// Add zoom Conrollers to map
		LinearLayout zoomLayout = (LinearLayout) findViewById(R.id.zoom);
		View zoomView = mapView.getZoomControls();
		zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		mapView.displayZoomControls(true);
		mapView.invalidate();
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			ArrayList<Location> locations = (ArrayList<Location>) extras
					.getSerializable("points");
			if (locations != null) {
				// Convet ArrayList<Loacation> in ArrayList<GeoPoint>
				ArrayList<GeoPoint> geoPoints = new ArrayList<GeoPoint>();
				Iterator<Location> iterator = locations.iterator();
				while (iterator.hasNext()) {
					Location location = iterator.next();
					geoPoints.add(new GeoPoint(
							(int) (location.getLatitude() * 1E6),
							(int) (location.getLongitude() * 1E6)));
				}
				DrawOverlays(mapView, geoPoints);
			} else {
				System.out.println("locations null");
			}
		} else {
			Toast.makeText(getApplicationContext(), "No points",
					Toast.LENGTH_SHORT).show();
		}
		/*
		 * // test code ArrayList<GeoPoint> points = new ArrayList<GeoPoint>();
		 * points.add(new GeoPoint((int) (50.001 * 1E6), (int) (30.001 * 1E6)));
		 * points.add(new GeoPoint((int) (50.002 * 1E6), (int) (30.002 * 1E6)));
		 * points.add(new GeoPoint((int) (50.003 * 1E6), (int) (30.003 * 1E6)));
		 * points.add(new GeoPoint((int) (50.004 * 1E6), (int) (30.004 * 1E6)));
		 * points.add(new GeoPoint((int) (50.005 * 1E6), (int) (30.005 * 1E6)));
		 * points.add(new GeoPoint((int) (50.006 * 1E6), (int) (30.006 * 1E6)));
		 * points.add(new GeoPoint((int) (50.007 * 1E6), (int) (30.007 * 1E6)));
		 * points.add(new GeoPoint((int) (50.008 * 1E6), (int) (30.008 * 1E6)));
		 * points.add(new GeoPoint((int) (50.009 * 1E6), (int) (30.009 * 1E6)));
		 * points.add(new GeoPoint((int) (50.010 * 1E6), (int) (30.010 * 1E6)));
		 * points.add(new GeoPoint((int) (50.011 * 1E6), (int) (30.011 * 1E6)));
		 * points.add(new GeoPoint((int) (50.012 * 1E6), (int) (30.012 * 1E6)));
		 * points.add(new GeoPoint((int) (50.013 * 1E6), (int) (30.013 * 1E6)));
		 * points.add(new GeoPoint((int) (50.014 * 1E6), (int) (30.014 * 1E6)));
		 * points.add(new GeoPoint((int) (50.015 * 1E6), (int) (30.015 * 1E6)));
		 * DrawOverlays(mapView, points);
		 */
		// /////
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	private void DrawOverlays(MapView map, ArrayList<GeoPoint> points) {
		// delete all overlays
		mapView.getOverlays().clear();
		if (points.size() > 0) {
			int currentColor = Color.RED;
			float currentRadius = 3;
			int colorStep = (int) (currentColor - Color.BLACK) / points.size();
			float radiusStep = (15 - currentRadius)  / points.size();

			Iterator<GeoPoint> iterator = points.iterator();
			GeoPoint start = iterator.next();
			GeoPoint finish = start;
			RouteOverLay overlay = new RouteOverLay(start, start, currentColor,
					currentRadius);
			mapView.getOverlays().add(overlay);
			GeoPoint previous = start;
			while (iterator.hasNext()) {
				currentColor -= colorStep;
				currentRadius += radiusStep;
				GeoPoint geopoint = iterator.next();
				RouteOverLay overlay1 = new RouteOverLay(previous, geopoint,
						currentColor, currentRadius);
				mapView.getOverlays().add(overlay1);
				previous = geopoint;
				finish = geopoint;
			}
			MapController mc = mapView.getController();
			mc.animateTo(start);
			mc.setZoom(19);
			mapView.invalidate();
		}
	}
}
